using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain.Dto.Output;
using RBAC.ErrorCode;
using RBAC.Write.Api.Application.Command.ERP.OutboundManagement;
using RBAC.Write.Api.Application.Dto.ERP;

namespace RBAC.Write.Api.Controllers.ERP
{
    public class DeleteDto
    {
        public long Id { get; set; }
    }

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutboundManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        public OutboundManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 新增销售出库（含明细）
        /// </summary>
        [HttpPost]
        public async Task<APIResult<long>> AddSalesOutbound([FromBody] AddSalesOutboundCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await _mediator.Send(request, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIResult<long>
                {
                    Code = APIEnums.Error,
                    Msg = $"新增销售出库失败: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 修改销售出库（删明细再插入）
        /// </summary>
        [HttpPost]
        public async Task<APIResult<bool>> UpdateSalesOutbound([FromBody] UpdateSalesOutboundCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await _mediator.Send(request, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIResult<bool>
                {
                    Code = APIEnums.Error,
                    Msg = $"修改销售出库失败: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 删除销售出库（仅未审核可删）
        /// </summary>
        [HttpPost]
        public async Task<APIResult<bool>> DeleteSalesOutbound([FromBody] DeleteDto dto, CancellationToken cancellationToken)
        {
            try
            {
                var command = new DeleteSalesOutboundCommand { Id = dto.Id };
                return await _mediator.Send(command, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIResult<bool>
                {
                    Code = APIEnums.Error,
                    Msg = $"删除销售出库失败: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 审核/反审 销售出库（传值：审批 或 开立）
        /// </summary>
        [HttpPost]
        public async Task<APIResult<bool>> AppOrUnappSalesOutbound([FromBody] AppOrUnappSalesOutboundCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await _mediator.Send(request, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIResult<bool>
                {
                    Code = APIEnums.Error,
                    Msg = $"设置销售出库状态失败: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 导出销售出库
        /// </summary>
        /// <param name="request">导出请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> ExportSalesOutbound([FromQuery] ExportSalesOutboundCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request, cancellationToken);
            if (result.Code != APIEnums.Success)
            {
                return BadRequest(result);
            }

            return File(
                result.Data,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                $"销售出库导出_{DateTime.Now:yyyyMMddHHmmss}.xlsx"
            );
        }
    }
}
